package com.gin.custom.cust1;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.gin.custom.R;

import java.util.List;

/**
 * Created by db.boy on 2018/6/11.
 */
public class CustomTabView extends LinearLayout {
    private List<String > titles;
    private List<View> popuViews;
    private LinearLayout tabView;
    private CustomPopupWindow customPopupWindow;
    private Context context;
    private int currentTabPosition = -1;

    public CustomTabView(Context context) {
        this(context,null);
    }

    public CustomTabView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CustomTabView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        if(!( context instanceof Activity)){
            throw new IllegalArgumentException("Context 不能非Activity");
        }
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.layout_custom_tab,this);
        tabView = findViewById(R.id.tabView);
    }
 
    public void setCustomTabViews(List<String> titles,List<View> views){
        if (titles.size() != views.size()) {
            throw new IllegalArgumentException("参数异常,tab与popuView的数量不相等..");
        }
        this.titles = titles;
        this.popuViews = views;
        if (tabView.getChildCount()>0) {
            tabView.removeAllViews();
        }
        initTab();
        
    }

    private void initTab() {
        View root;
        TextView tvTab;
        for (String tab : titles) {
            root = LayoutInflater.from(getContext()).inflate(R.layout.item_tab_view, tabView, false);
            root.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f));
            tvTab = root.findViewById(R.id.tv_title);
            tvTab.setText(tab);
            root.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    selectTab(v);
                }
            });
            tabView.addView(root);
        }
    }

    private void selectTab(View v) {
        Log.i("------","v: "+v);
            ViewGroup tmp;
            for (int i = 0; i < tabView.getChildCount(); i++) {
                //0,2,4,6
                tmp = (ViewGroup) tabView.getChildAt(i);
                //选中的为当前的
                if (v == tmp) {
                    if (currentTabPosition == i) {
                        //选中项,当前显示项,关闭
                        closeTab();
                    } else {
                        if (currentTabPosition == -1) {
                            //从未选中过
                            showOrClosePopu(true);
                        }
                        currentTabPosition = i;

                        customPopupWindow = new CustomPopupWindow((Activity) context,popuViews.get(i));
                        // selectView设置为选中样式
                        setTabSytle(tmp, true);
                        customPopupWindow.show(v);
                        customPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                            @Override
                            public void onDismiss() {
                                currentTabPosition = -1;
                            }
                        });
                    }

                } else {
                    //非选中项
//                    setTabSytle(tmp, false);
                }

            }
    }

    private void setTabSytle(ViewGroup tmp, boolean b) {
        
    }

    private void showOrClosePopu(boolean b) {
        
    }

    private void closeTab() {
        
    }
}
